﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GAMS.MODEL;
using KPIS.DBM;
using System.Data;
using System.Configuration;

namespace KPIS.GERP.GAMS.DAL
{
    public class D_ACM_BOOK_Bank
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_ACM_BOOK_Bank> GetList() 
        {
            DataRowCollection drc = null;
            IList<M_ACM_BOOK_Bank> infoList = new List<M_ACM_BOOK_Bank>();
            try
            {
                dbManager.Open();

                string strSQL = " SELECT bank_acc_seq, bank_acc_no, bank_acc_name, fba.account_seq"
                    + ", bb.bank_name, concat(coalesce(account_code, ''), ' : ', coalesce(account_name, '')) as acc_name"
                              + " FROM fim_mas_bank_accounts fba"
                              + " left join fim_mas_bank_branchs fmb on fba.branch_seq = fmb.branch_seq"
                              + " left join fim_mas_banks bb on fmb.bank_seq = bb.bank_seq"
                              + " left join acm_mas_account acc on fba.account_seq = acc.account_seq "
                              + " and acc.record_status = 'U' "
                              + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq "
                              + " and acc_ver.record_status = 'U' "
                              + " and acc_ver.use_flag = 1 "
                              + " WHERE fba.record_status not like 'D'"
                              + " ORDER BY bank_acc_no";
                
                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_BOOK_Bank info = new M_ACM_BOOK_Bank();
                        info.bank_acc_seq = Convert.ToInt32(drc[i]["bank_acc_seq"]);
                        info.account_seq = cn.ConvertNullInt(drc[i]["account_seq"]);
                        info.account_code_name = drc[i]["acc_name"].ToString();
                        info.bank_acc_no = drc[i]["bank_acc_no"].ToString();
                        info.bank_acc_name = drc[i]["bank_acc_name"].ToString();
                        info.bank_name = drc[i]["bank_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public M_ACM_BOOK_Bank GetListFillBySeq(Nullable<int> bank_acc_seq)
        {
            string strSQL ;
            DataRowCollection drc = null;
            M_ACM_BOOK_Bank info = new M_ACM_BOOK_Bank();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@bank_acc_seq", bank_acc_seq);

                strSQL = " SELECT bank_acc_seq,bank_acc_no,bank_acc_name,fba.account_seq,bb.bank_name ,acc.account_code"
                              + " ,concat(coalesce(account_code, ''), ' : ', coalesce(account_name, '')) as acc_name "
                              + " FROM   fim_mas_bank_accounts fba "
                              + " left join fim_mas_bank_branchs fmb on fba.branch_seq = fmb.branch_seq"
                              + " left join fim_mas_banks bb on fmb.bank_seq = bb.bank_seq"
                              + " left join  acm_mas_account acc on fba.account_seq = acc.account_seq "
                              + " and acc.record_status = 'U' "
                              + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq "
                              + " and acc_ver.record_status = 'U' "
                              + " and acc_ver.use_flag = 1 "
                              + " WHERE fba.record_status not like 'D'"
                              + " and bank_acc_seq = @bank_acc_seq "
                              + " ORDER BY bank_acc_no";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bank_acc_seq = Convert.ToInt32(drc[0]["bank_acc_seq"]);
                    info.account_seq = cn.ConvertNullInt(drc[0]["account_seq"]);
                    info.account_code = drc[0]["account_code"].ToString();
                    info.bank_acc_no = drc[0]["bank_acc_no"].ToString();
                    info.bank_acc_name = drc[0]["bank_acc_name"].ToString();
                    info.bank_name = drc[0]["bank_name"].ToString();
                    info.account_code_name = drc[0]["acc_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        //public M_ACM_BOOK_Bank GetListFillByAccountSeq(Nullable<int> account_seq)
        //{
        //    string strSQL;
        //    DataRowCollection drc = null;
        //    M_ACM_BOOK_Bank info = new M_ACM_BOOK_Bank();
        //    try
        //    {
        //        dbManager.Open();
        //        dbManager.CreateParameters(1);
        //        dbManager.AddParameters(0, "@account_seq", account_seq);

        //        strSQL = " SELECT account_seq,account_code,account_name ,concat(coalesce(account_code, ''), ' : ', coalesce(account_name, '')) as acc_name"
        //                +" FROM  acm_mas_account "
        //                + " WHERE account_seq=@account_seq ";

        //        drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
        //        if (drc.Count > 0)
        //        {
        //            info.account_code = drc[0]["account_code"].ToString();
        //            info.bank_acc_name = drc[0]["account_name"].ToString();
        //            info.account_code_name = drc[0]["acc_name"].ToString();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw (ex);
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }

        //    return info;
        //}

        //public IList<M_ACM_BOOK_Bank> GetListAccount()
        //{
        //    DataRowCollection drc = null;
        //    IList<M_ACM_BOOK_Bank> infoList = new List<M_ACM_BOOK_Bank>();
        //    try
        //    {
        //        dbManager.Open();
        //        //string strSQL = " SELECT account_seq,account_code,account_name,concat(coalesce(account_code, ''), ' : ', coalesce(account_name, '')) as acc_name "
        //        //        + " FROM  acm_mas_account "
        //        //        + " ORDER BY account_code ";
        //        string strSQL = " SELECT account_seq,account_code,account_name ,concat(coalesce(account_code, ''), ' : ', coalesce(account_name, '')) as acc_name "
        //                      + " FROM  acm_mas_account acm left join acm_mas_account_version acmv on (acm.version_seq = acmv.version_seq) and (acmv.record_status=true) and (acmv.use_flag =true) "
        //                      + " WHERE (upper_account_seq is null) and (acm.record_status=true) "
        //                      + " ORDER BY acc_name ";

        //        drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
        //        if (drc.Count > 0)
        //        {
        //            for (int i = 0; i < drc.Count; i++)
        //            {
        //                M_ACM_BOOK_Bank info = new M_ACM_BOOK_Bank();
        //                info.account_seq = Convert.ToInt32(drc[i]["account_seq"]);
        //                info.account_code = drc[i]["account_code"].ToString();
        //                info.account_code_name = drc[i]["acc_name"].ToString();
        //                infoList.Add(info);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw (ex);
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }
        //    return infoList;
        //}

        public void Update(M_ACM_BOOK_Bank info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@account_seq", info.account_seq);
                dbManager.AddParameters(1, "@bank_acc_seq", info.bank_acc_seq);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);


                strSQL = "UPDATE fim_mas_bank_accounts"
                    + "  SET account_seq = @account_seq"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + "  WHERE bank_acc_seq = @bank_acc_seq";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public DataTable Report_BOOK_Bank()
        {
            string strSQL;
            DataTable dtb = null;
            try
            {
                dbManager.Open();

                strSQL = "SELECT bank_acc_seq,bank_acc_no,bank_acc_name,fba.account_seq,fmb.bank_name,account_code,account_name"
                    + " FROM fim_mas_bank_accounts fba "
                    + " left join fim_mas_bank_branchs bb on fba.branch_seq = bb.branch_seq "
                    + " left join fim_mas_banks fmb on bb.bank_seq = fmb.bank_seq "
                    + " left join acm_mas_account acc on fba.account_seq = acc.account_seq "
                    + " and acc.record_status = 'U' "
                    + " left join acm_mas_account_version acc_ver on acc.version_seq = acc_ver.version_seq "
                    + " and acc_ver.record_status = 'U' "
                    + " and acc_ver.use_flag = 1 "
                    + " WHERE fba.record_status not like 'D'"
                    + " ORDER BY bank_acc_no";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return dtb;
        }
   
    }
}
